//
//  peopleVC.m
//  YaoHeAPP
//
//  Created by xyh on 16/3/9.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "peopleVC.h"
#import "Controller.h"
#import "Masonry.h"
#import "peopleModel.h"
#import "CWStarRateView.h"
#import "UIImageView+WebCache.h"
#import "Define.h"
#import "SVPullToRefresh.h"
#import "peopleDetails.h"
#import "labelview.h"
@interface peopleVC ()
{
    NSMutableArray*ary;
    UIView *navcBar;
    NSMutableArray*_dataArray;//数据源数组
    labelview *la;
}

@property(nonatomic,strong) UITableView *tableview;

@end

@implementation peopleVC

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"peopleVC"];
    
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"peopleVC"];
    
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor=bgVCcolor;

    //详情页移除了相关数据
    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(PeopleDetailsChangedState) name:@"PeopleDetails_view_deletail" object:nil];

    _dataArray=[[NSMutableArray alloc]init];
    
    //创建表格
    _tableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-100) style: UITableViewStylePlain];
    _tableview.dataSource=self;
    _tableview.delegate=self;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableview.backgroundColor=bgVCcolor;
//    _tableview.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//确保TablView能够正确的调整大小
    
    //_tableView.scrollEnabled=NO;
    [self.view addSubview:_tableview];
    
    
    //放置下拉上拉刷新控件
    __weak peopleVC *weakSelf = self;
    
    // setup pull-to-refresh
    [self.tableview addPullToRefreshWithActionHandler:^{
        [weakSelf insertRowAtTop];
    }];
    
//    // setup infinite scrolling
//    [self.tableview addInfiniteScrollingWithActionHandler:^{
//        [weakSelf insertRowAtBottom];
//    }];
    
    [self insertRowAtTop];
 
}

-(void)PeopleDetailsChangedState
{

[self insertRowAtTop];

}


-(void)insertRowAtTop
{

    __weak peopleVC *weakSelf = self;
    
    [weakSelf.tableview beginUpdates];

    [Controller requestWithURL:@"AppUserCenter/PostQueryStaffInfo?from=app" jsondic:nil finishBlock:^(ClassData *classData) {
        if ([classData.number integerValue]==0) {
            ary=classData.data;
            
             [_dataArray removeAllObjects];
            for (NSInteger i=0; i<ary.count; i++) {
                peopleModel*peoMD=[[peopleModel alloc]initWith:ary[i]];
                [_dataArray addObject:peoMD];
            }
            
            //判断 是否  有 无数据
            if (_dataArray.count==0) {
                if (la) {
                    [la removeFromSuperview];
                }
                
                la=[[labelview alloc]init];
                [la setimage:@"Nodata_wenhao" tit:@"暂无数据" buttontit:nil frem:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                la.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
                la.image.frame=CGRectMake((self.view.frame.size.width-100)/2, 50, 100, 125);
                la.label.frame=CGRectMake(50, 180, self.view.frame.size.width-100, 30);
                [_tableview addSubview:la];
            }
            
            [self.tableview reloadData];
        }
        else{
            ALERTSHOW(classData.message)
        }
        
    }];


    
    // 3. 结束刷新(相当于动画)
    [weakSelf.tableview endUpdates];
    
    
    [weakSelf.tableview.pullToRefreshView stopAnimating];

    

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 110;
}

//每个分区单元格的个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    peopleModel*peoMD=_dataArray[indexPath.row];
    UITableViewCell*cell=[tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    if (cell==nil) {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
        
        //头像
        UIImageView*headImg=[[UIImageView alloc]init];
        headImg.frame=CGRectMake(15, 30, 50, 50);
        headImg.layer.masksToBounds=YES;
        headImg.layer.cornerRadius=25;
        headImg.tag=10;
        [cell addSubview:headImg];
        
        //公司名
        UILabel*label1=[[UILabel alloc]init];
        label1.font=font14;
        label1.textColor=[UIColor colorWithRed:102.f/255.f green:102.f/255.f blue:115.f/255.f alpha:1];
        label1.tag=11;
        [cell addSubview:label1];
        label1.frame=CGRectMake(70, 20, 200, 20);
        //评分
        UILabel*starts=[[UILabel alloc]init];
        [cell addSubview:starts];
        starts.frame=CGRectMake(70, 45, 120, 20);
        
        
        //评分
        CWStarRateView*stars=[[CWStarRateView alloc]initWithFrame:CGRectMake(0,0, 80, 20) numberOfStars:5];
        stars.scorePercent=[peoMD.SERVICE_SCORE floatValue]/5;
        stars.allowIncompleteStar = YES;
        stars.hasAnimation =YES;
        [starts addSubview:stars];
        UILabel*score=[[UILabel alloc]init];
        score.text=[NSString stringWithFormat:@"%.2f分",peoMD.SERVICE_SCORE.floatValue];
        score.textColor=[UIColor orangeColor];
        score.textAlignment=NSTextAlignmentLeft;
        score.font=font14;
        [starts addSubview:score];
        //        [score mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.left.equalTo(stars.mas_right).offset(2);
        //            make.top.offset(0);     make.bottom.offset(0);
        //        }];
        
        score.frame=CGRectMake(100, 0, 50, 20);
        
        //手机图片
        UIImageView *iponv=[[UIImageView alloc]init];
        iponv.image=[UIImage imageNamed:@"iphone"];
//        iponv.backgroundColor=[UIColor grayColor];
        iponv.frame=CGRectMake(70, 70, 14, 20);
        [cell addSubview:iponv];
        //抢单次数成功次数
        UILabel*label2=[[UILabel alloc]init];
        label2.textColor=[UIColor colorWithRed:102.f/255.f green:102.f/255.f blue:115.f/255.f alpha:1];
        label2.font=font12;
        label2.tag=13;
        [cell addSubview:label2];
        //        [label2 mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.left.offset(70); make.height.offset(20);make.top.offset(70);
        //        }];
        label2.frame=CGRectMake(70+20, 70, 200, 20);
        
        UIImageView*lineImg1=[[UIImageView alloc]init];
        lineImg1.frame=CGRectMake(0, 109.5, self.view.frame.size.width, 0.5);
        lineImg1.backgroundColor=[UIColor lightGrayColor];
        [cell addSubview:lineImg1];
        
        
        
    }
    
    //NSString*string=@"123456";
    
    //头像
    UIImageView*headImage=(UIImageView*)[cell viewWithTag:10];
    [headImage sd_setImageWithURL:[NSURL URLWithString:peoMD.LOGO_PATH] placeholderImage:[UIImage imageNamed:@"default_head"]];
    //人名
    UILabel*companyName=(UILabel*)[cell viewWithTag:11];
    companyName.text=peoMD.REG_NAME;
    //评分
    CWStarRateView*starsScore=(CWStarRateView*)[cell viewWithTag:12];
    starsScore.scorePercent=[peoMD.SERVICE_SCORE floatValue]/5 ;
    //手机号拼接
    UILabel*GrabASingle=(UILabel*)[cell viewWithTag:13];
    GrabASingle.text=[NSString stringWithFormat:@"%@  业绩：%@单",peoMD.REG_MOBILE,peoMD.PURCHASE_NUM];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
   //进入职员详情页面;
    
    peopleModel*peoMD=_dataArray[indexPath.row];
    
    peopleDetails*peoDvc=[peopleDetails new];
    peoDvc.userID=peoMD.USER_ID;
    peoDvc.CompayID=peoMD.COMPANY_ID;
    [self.navigationController pushViewController:peoDvc animated:YES];

    
    
    
}


//返回上个界面
-(void)backUp
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
